package com.study.threadpool.service;

import com.study.threadpool.task.LoveTask;
import org.springframework.stereotype.Service;

import java.util.concurrent.CountDownLatch;

/**
 * @author flwu
 * @date 2020/3/11 6:51 PM
 * @MOTTO 保持空和敬畏
 */
@Service
public class CountDownLatchService {

    public void test1() {
        CountDownLatch count = new CountDownLatch(2);

        Thread thread1 = new Thread(() -> {
            try {
                System.out.println("kiss you");
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                count.countDown();
            }
        });
        Thread thread2 = new Thread(() -> {
            try {
                System.out.println("kiss me");
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                count.countDown();
            }
        });
        thread1.start();
        thread2.start();

        try {
            System.out.println("两个线程都已经启动");
            count.await();
            System.out.println("两个线程都已经结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("done");
    }


}
